package com.ayi.servlet.base; /**
 * @author Ayingzz
 * @create 2021/11/28 23:23
 */

import com.ayi.bean.User;
import com.ayi.dao.bookdao.QueryBook;
import com.ayi.dao.bookdao.QueryTypes;
import com.ayi.dao.cartdao.QueryCart;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "IndexServlet", value = "/IndexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 准备好详情展示状态map
        Map<Integer, Integer> detailsShowMap = new HashMap<>();
        detailsShowMap.put(1, -999);
        request.getSession().setAttribute("detailsShowMap", detailsShowMap);

        // 显示所有图书(列表处)
        request.getSession().setAttribute("books", new QueryBook().queryBooksByTypeAndName("全部类型", ""));

        // 准备好所有类型
        request.getSession().setAttribute("types", new QueryTypes().queryTypes(-1));

        // 准备好默认类型选择
        request.getSession().setAttribute("typeSelected", "全部类型");

        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            request.getSession().setAttribute("cartList", "");
        } else {
            // 所有图书(订单处)
            request.getSession().setAttribute("booksAll", new QueryBook().queryBooksIntoCartSettingNum(user.getUsername()));
            // 购物车
            request.getSession().setAttribute("cartList", new QueryCart().queryCartByUsername(user.getUsername()));
        }

        // 回到index.jsp
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

}
